﻿using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using System;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class CourseAdjustEditorTabAdapter : EditorAdaptor
    {
        public override Type ElementType => typeof(CourseAdjust);

        public override string ItemName => "调换课安排";

        public override string API => "apis/v1/course_adjusts";

        public override string PkField => "FromDate";

        public override string NameField => "FromDate";

        public override bool AllowIndexItem => Permission.CheckPermission(Permission.CourseAdjust.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.CourseAdjust.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.CourseAdjust.Update);

        public override bool AllowDeleteItem => Permission.CheckPermission(Permission.CourseAdjust.Delete);

        private ToolTip TT_Main;
        private FormPanel FP_Main;
        private DateTimePicker DTP_FromDate;
        private CheckBox CB_Holiday;
        private DateTimePicker DTP_ToDate;
        private CheckBox CB_Status;



        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            TT_Main = new ToolTip();
            FP_Main = new FormPanel();
            _SettingFormPanel(editorPanel, FP_Main);

            DTP_FromDate = new MyDateTimePicker();
            DTP_FromDate.DataBindings.Add("Value", source, "FromDateRaw");
            FP_Main.AddField("源始日期", DTP_FromDate);
            TT_Main.SetToolTip(DTP_FromDate,"被放假的日期,设置并启用该项后,此日期将被视为假期.");

            CB_Holiday = new CheckBox()
            {
                Text = "源始日期属于假期"
            };
            CB_Holiday.DataBindings.Add("Checked", source, "IsHoliday");
            CB_Holiday.CheckedChanged += (sender, e) =>
            {
                DTP_ToDate.Enabled = !CB_Holiday.Checked;
            };
            TT_Main.SetToolTip(CB_Holiday, "表明当前是换课还是直接放假");
            FP_Main.AddField("是否假期", CB_Holiday);

            DTP_ToDate = new MyDateTimePicker();
            DTP_ToDate.DataBindings.Add("Value", source, "ToDateRaw");
            TT_Main.SetToolTip(DTP_ToDate, "被放假的日期的课程将会安排到此日期上");
            FP_Main.AddField("目标日期", DTP_ToDate);

            CB_Status = new CheckBox
            {
                Text = "启用"
            };
            TT_Main.SetToolTip(CB_Status, "表明该条记录是否启用");
            CB_Status.DataBindings.Add("Checked", source, "Status");
            FP_Main.AddField("是否启用", CB_Status);

        }

        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    DTP_FromDate.Enabled = CB_Holiday.Enabled = DTP_ToDate.Enabled = CB_Status.Enabled = true;
                    break;
                case DisplayMode.Edit:
                    DTP_FromDate.Enabled = false;
                    DTP_ToDate.Enabled = CB_Holiday.Enabled = CB_Status.Enabled = true;
                    break;
                case DisplayMode.Preview:
                     DTP_FromDate.Enabled =CB_Holiday.Enabled= DTP_ToDate.Enabled = CB_Status.Enabled = false;
                    break;
                case DisplayMode.None:
                    FP_Main.ClearContent();
                    goto case DisplayMode.Preview;
                default:
                    return false;
            }
            return true;
        }

        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }
}
